package com.kitty.spring.annotationdriven;

import com.kitty.spring.annotationdriven.beans.Person;
import com.kitty.spring.annotationdriven.configuration.BeanConfig;
import com.kitty.spring.annotationdriven.configuration.BeanConfig1;
import com.kitty.spring.annotationdriven.configuration.BeanConfig2;
import com.kitty.spring.annotationdriven.configuration.BeanConfigLifeStylr;
import com.kitty.spring.annotationdriven.factory.MyFactoryBean;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.env.Environment;

import java.util.Map;

public class MainTest1 {

    AnnotationConfigApplicationContext context;

    @Before
    public void before(){
        //this.context = new AnnotationConfigApplicationContext(BeanConfig1.class);
    }

    @Test
    public  void scopeTest(){
        this.context = new AnnotationConfigApplicationContext(BeanConfig1.class);
        Person person = context.getBean(Person.class);
        //Person person1 = context.getBean(Person.class);

        //System.out.println(person == person1);
    }

    @Test
    public  void lazyTest(){
        //进行容器初始化的时候并不会创建bean
        this.context = new AnnotationConfigApplicationContext(BeanConfig1.class);

        //在调用getBean方法的时候才会初始相关对象，并放到容器中，如果为单实例，那么只会被创建一次
        //Person person = context.getBean(Person.class);
    }

    @Test
    public  void conditionTest(){
        this.context = new AnnotationConfigApplicationContext(BeanConfig1.class);
        String[] beanNamesForType = context.getBeanNamesForType(Person.class);
        for (String beanName : beanNamesForType) {
            System.out.println("beanName = " + beanName);
        }
        Map<String, Person> beans = context.getBeansOfType(Person.class);
        System.out.println("beans = " + beans);
    }


    @Test
    public  void importTest(){
        this.context = new AnnotationConfigApplicationContext(BeanConfig2.class);
        String[] definitionNames = context.getBeanDefinitionNames();
        for (String definitionName : definitionNames) {
            System.out.println("definitionName = " + definitionName);
        }
    }


    @Test
    public  void myFactoryBeanTest(){
        this.context = new AnnotationConfigApplicationContext(BeanConfig2.class);
        String[] definitionNames = context.getBeanDefinitionNames();
        for (String definitionName : definitionNames) {
            System.out.println("definitionName = " + definitionName);
        }

        MyFactoryBean bean = context.getBean(MyFactoryBean.class);

        Object bean1 = context.getBean("myFactoryBean");

    }

    @Test
    public  void beanLifeCycleTest(){
        this.context = new AnnotationConfigApplicationContext(BeanConfigLifeStylr.class);
        String[] definitionNames = context.getBeanDefinitionNames();
        for (String definitionName : definitionNames) {
            System.out.println("definitionName = " + definitionName);
        }

        context.close();

    }



}
